<template>
  <div class="user-detail">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="手机">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="入职时间">
        <el-date-picker v-model="form.correctionTime" type="date" placeholder="选择日期" style="width: 100%;" />
      </el-form-item>
      <el-form-item label="员工头像">
        <!-- <img :src="form.staffPhoto" alt="图片没有显示出来的替代文字" title="鼠标移动上去显示的文字" width="100"> -->
        <UploadImg :key="form.id" :src="form.staffPhoto" @upload-success="uploadSucces" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存更新</el-button>
        <el-button>返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getUserInfoRequest, updateUserInfoRequest } from '@/api/employees'
export default {
  name: '',
  data() {
    return {
      form: {
        mobile: '',
        correctionTime: '',
        staffPhoto: ''
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    async getUserInfo() {
      try {
        const res = await getUserInfoRequest(this.$route.query?.id || '')
        if (res.success) {
          this.form = res.data
        }
      } catch (err) {
        console.log('updateUserInfoRequest: ', err)
      }
    },
    async updateUserInfo() {
      try {
        const res = await updateUserInfoRequest(this.form)
        if (res.success) {
          this.$router.back()
        } else {
          this.$message.error('更新个人信息失败')
        }
      } catch (err) {
        console.log('updateUserInfoRequest: ', err)
      }
    },
    onSubmit() {
      this.updateUserInfo()
    },
    uploadSucces(res) {
      // 这个函数的参数，就是子组件向父组件传递过来的值
      console.log('父组件接收到子组件的值：', res)
      this.form.staffPhoto = res
      console.log(this.form)
    }
  }
}
</script>
